package com.chen.controller;

import com.alibaba.fastjson.JSONObject;
import jdk.nashorn.internal.objects.Global;
import org.apache.commons.io.FileUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;


@Controller
@RequestMapping("/blog")
public class FileController {

    //上传文件
    @RequestMapping("/uploadFile")
    @ResponseBody
    public String uploadFile(HttpServletRequest request, HttpServletResponse response,
                             @RequestParam(value = "editormd-image-file", required = false) MultipartFile file) {

        //获取当前文件名
        String trueFileName = file.getOriginalFilename();

        //获取文件后缀如：.jpg.
        String suffix = trueFileName.substring(trueFileName.lastIndexOf("."));

        //重命名
        String fileName = UUID.randomUUID().toString().replace("-", "") + suffix;

        //发布后的地址
        String path = request.getSession().getServletContext().getRealPath("/common/upload/");

        File targetFile = new File(path, fileName);
        System.out.println(targetFile);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }

        //保存
        try {
            file.transferTo(targetFile);
        } catch (Exception e) {
            e.printStackTrace();
        }


        JSONObject res = new JSONObject();
        //这就是返回给页面的json数据
        res.put("url", request.getContextPath() + "/common/upload/" + fileName);
        res.put("success", 1);
        res.put("message", "upload success!");

        return res.toString();
    }

    //上传博客封面
    @ResponseBody
    @RequestMapping(value = "/uploadPic",method = RequestMethod.POST)
    public Map<String, Object> uploadFile(HttpServletRequest servletRequest, @RequestParam("file") MultipartFile file)
            throws IOException {
        // 如果文件内容不为空，则写入上传路径
        if (!file.isEmpty()) {
            // 上传文件路径
            String UploadPath = "F:\\Idea Project\\system\\blog_admin\\web\\common\\upload\\";
            //取得原文件名字
            String fileName = file.getOriginalFilename();
            //取得文件扩展名
            String suffix = fileName.substring(fileName.lastIndexOf("."));
            //提取系统时间作为新文件名
            String prefix = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date().getTime());
            //保存路径
            // 上传文件名
            String filename = prefix+suffix;
            File filepath = new File(UploadPath, filename);
            // 判断路径是否存在,没有创建
            if (!filepath.getParentFile().exists()) {
                filepath.getParentFile().mkdirs();
            }
            // 将上传文件保存到一个目标文档中
            File file1 = new File(UploadPath + File.separator + filename);
            System.out.println(file1);
            file.transferTo(file1);
            Map<String, Object> res = new HashMap<>();
            // 返回的是一个url对象,图片名称
            res.put("url", "../common/upload/"+filename);
            return res;
        } else {
            return null;
        }
    }

}
